<?php
/**
 * Created by PhpStorm
 * User: whh
 * Date: 2022/8/15
 * Time: 17:12
 */

namespace app\common\model;


trait Common
{

    /**
     * 获取列表信息
     * @param array $where
     * @param string $field
     * @param string $orderBy
     * @param array $with
     * @param string $groupBy
     * @param int $page
     * @param int $pageSize
     * @return mixed
     * @Author: whh
     */
    public static function getList($where = [], $field = '*', $orderBy = '', $page = 0, $limit = 0, $with = [], $groupBy = '')
    {
        if (!empty($with)) {
            $query = self::field($field)->with($with)->where($where);
        } else {
            $query = self::field($field)->where($where);
        }
        if (!empty($orderBy)) {
            $query = $query->order($orderBy);
        }
        if (!empty($groupBy)) {
            $query = $query->group($groupBy);
        }
        if ($page != 0 && $limit != 0) {
            $query = $query->page($page)->limit($limit);
        }
        return $query->select();
    }

    /**
     * 获取数量
     * @param array $where
     * @param string $groupBy
     * @return mixed
     * @Author: whh
     */
    public static function getCount($where = [], $groupBy = '')
    {
        $query = self::where($where);

        if (!empty($groupBy)) {
            $query = $query->group($groupBy);
        }
        return $query->count();
    }
}